package com.canbot.u05.activity.presidemode;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.canbot.u05.R;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.utils.ToastTools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * 选择移动参数选择
 */
public class PresideMoveSetting extends BaseCompereActivity {

        @BindView(R.id.preside_cancel_iv)
        ImageView presideCancelIv;

        @BindView(R.id.preside_sure_iv)
        ImageView presideSureIv;

        @BindView(R.id.preside_direct_tv)
        TextView presideDirectTv;

        @BindView(R.id.preside_num_et)
        EditText presideNumEt;

        @BindView(R.id.preside_unit_tv)
        TextView presideUnitTv;

        @BindView(R.id.preside_speed_tv)
        TextView presideSpeedTv;

        @BindView(R.id.preside_speed_1)
        TextView presideSpeed1;

        @BindView(R.id.preside_speed_2)
        TextView presideSpeed2;

        @BindView(R.id.preside_speed_3)
        TextView presideSpeed3;

        private TextView currentSelect;

        private String direct;

        private String num;

        private String speed;

        public static boolean isFirstMove;

        public static boolean isFirstTurn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_move_settings);
                ButterKnife.bind(this);
                initView();
        }

        private void initView() {
                Intent intent = getIntent();
                direct = intent.getStringExtra("direct");
                num = intent.getStringExtra("num");
                speed = intent.getStringExtra("speed");
                if (TextUtils.isEmpty(direct)) {
                        direct = "前进";
                }
                switch (direct) {
                        case "前进":
                        case "后退":
                                if (isFirstMove) {
                                        isFirstMove = false;
                                        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(24));
                                }
                                presideDirectTv.setText(direct + "距离");
                                presideUnitTv.setText("米");
                                break;
                        case "左转":
                        case "右转":
                                if (isFirstTurn) {
                                        isFirstTurn = false;
                                        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(25));
                                }
                                presideDirectTv.setText(direct + "角度");
                                presideUnitTv.setText("度");
                                break;
                }
                presideNumEt.addTextChangedListener(watcher);
                presideNumEt.setText(num);
                presideNumEt.setSelection(presideNumEt.length());
                if (TextUtils.isEmpty(speed)) {
                        speed = "正常";
                }
                switch (speed) {
                        case "慢速":
                                currentSelect = presideSpeed1;
                                presideSpeed1.setSelected(true);
                                break;
                        case "正常":
                                currentSelect = presideSpeed2;
                                presideSpeed2.setSelected(true);
                                break;
                        case "快速":
                                currentSelect = presideSpeed3;
                                presideSpeed3.setSelected(true);
                                break;
                        default:
                                currentSelect = presideSpeed2;
                                presideSpeed2.setSelected(true);
                                break;
                }

        }

        private TextWatcher watcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                        if (TextUtils.isEmpty(s)) {
                                return;
                        }
                        int integer = Integer.valueOf(s.toString());
                        switch (direct) {
                                case "前进":
                                case "后退":
                                        if (Math.abs(integer) > 10) {
                                                presideNumEt.setText("10");
                                                ToastTools.newInstance(PresideMoveSetting.this).showText("最大移动距离为10米");
                                        }
                                        break;
                                case "左转":
                                case "右转":
                                        if (Math.abs(integer) > 360) {
                                                presideNumEt.setText("360");
                                                ToastTools.newInstance(PresideMoveSetting.this).showText("最大旋转角度为360度");
                                        }
                                        break;
                                default:

                        }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };


        @OnClick({R.id.preside_cancel_iv, R.id.preside_sure_iv, R.id.preside_speed_1, R.id.preside_speed_2, R.id.preside_speed_3})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.preside_cancel_iv:
                                finish();
                                break;
                        case R.id.preside_sure_iv:
                                String text = presideNumEt.getText().toString().trim();
                                if (TextUtils.isEmpty(text)) {
                                        ToastTools.newInstance(this).showText(getString(R.string.parameter_empty));
                                        return;
                                }
                                try {
                                        if (Float.parseFloat(text) < 0) {
                                                ToastTools.newInstance(this).showText(getString(R.string.forward_distance_negative));
                                                return;
                                        }
                                } catch (NumberFormatException e) {
                                        e.printStackTrace();
                                        ToastTools.newInstance(this).showText(getString(R.string.wrong_moving_parameter));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("num", text);
                                intent.putExtra("speed", currentSelect.getText().toString());
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                        case R.id.preside_speed_1:
                                if (currentSelect != presideSpeed1) {
                                        currentSelect.setSelected(false);
                                        currentSelect = presideSpeed1;
                                        presideSpeed1.setSelected(true);
                                }
                                break;
                        case R.id.preside_speed_2:
                                if (currentSelect != presideSpeed2) {
                                        currentSelect.setSelected(false);
                                        currentSelect = presideSpeed2;
                                        presideSpeed2.setSelected(true);
                                }
                                break;
                        case R.id.preside_speed_3:
                                if (currentSelect != presideSpeed3) {
                                        currentSelect.setSelected(false);
                                        currentSelect = presideSpeed3;
                                        presideSpeed3.setSelected(true);
                                }
                                break;
                }
        }

}
